package com.dtagri.fjwlw.ui;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;

import com.dtagri.fjwlw.util.TDVideoHelper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * 天地伟业
 * @author Administrator
 *
 */
public class VView extends View implements Runnable{

	Bitmap  mSCBitmap = null;   
	private int width = 100;  // �˴��趨��ͬ�ķֱ���
	private int height = 100;
	private int m_iScreenWidth = 1;	//��ǰ��Ļ�ֱ���
	private int m_iScreenHeight = 1;
	private int m_iRealVideoWidth = 1;   //ʵ�ʻ����Ƶ�ֱ���?
	private int m_iRealVideoHeight = 1;
	Matrix m_Matrix;   //��Ƶ����
	private float m_fScaleHeight,m_fScaleWidth;
	public byte[] mPixel = new byte[172*140*2];
	private long mLastTime,mCurTime;
//	public static int state=0;//0竖屏 1横屏
	public ByteBuffer buffer = ByteBuffer.wrap( mPixel );
	Bitmap VideoBit = Bitmap.createBitmap(width, height, Config.RGB_565);
	public VView(Context context,AttributeSet att) {
		super(context,att);
		setFocusable(true);

		m_Matrix = new Matrix();
		m_fScaleHeight = 1.0f;
		m_fScaleWidth = 1.0f;
		for(int i=0; i<mPixel.length; i++)
		{
			mPixel[i]=(byte)0x00;
		}
//		this.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//				mLastTime=mCurTime;
//				mCurTime=System.currentTimeMillis();
//				if(mCurTime-mLastTime<1000){
//
//					if(state==0){
//						Toast.makeText(getContext(), "从竖屏切换为横屏 ", 500).show();
//						state=1;
//						Message msg=new Message();
//						msg.arg1=88;
//						helper.msgHandler.sendMessage(msg);
//
//					}else{
//						Toast.makeText(getContext(), "从横屏切换为竖屏 ", 500).show();
//						state=0;
//						Message msg=new Message();
//						msg.arg1=89;
//						helper.msgHandler.sendMessage(msg);
//					}
//
//				}
//
//			}
//		});
	}
	public boolean SetCurrentScreenSize(int _iWidth, int _iHeight)
	{
		if(_iWidth <= 0 || _iHeight <= 0)
			return false;
		m_iScreenWidth = _iWidth;
		m_iScreenHeight = _iHeight;
		SetScale();
		return true;
	}
	public boolean SetRealVideoSize(int _iWidth, int _iHeight)
	{
		if(_iWidth <= 0 || _iHeight <= 0)
			return false;
		m_iRealVideoWidth = _iWidth;
		m_iRealVideoHeight = _iHeight;
		mPixel = new byte[(int)(_iWidth*_iHeight*2)];
		buffer = ByteBuffer.wrap( mPixel );
		VideoBit = Bitmap.createBitmap(_iWidth, _iHeight, Config.RGB_565);
		SetScale();
		return true;
	}
	/*
	 * �ж���Ƶ��С�Ƿ����?
	 */
	public boolean IsEqualVideoSize(int _iWidth, int _iHeight)
	{
		if( (m_iRealVideoWidth == _iWidth) && (m_iRealVideoHeight == _iHeight) )
		{
			return true;
		}

		return false;
	}

	private void SetScale()
	{
		m_fScaleWidth = (float)m_iScreenWidth/m_iRealVideoWidth;
		m_fScaleHeight = (float)m_iScreenHeight/m_iRealVideoHeight;
		m_Matrix.reset();
		m_Matrix.postScale(m_fScaleWidth, m_fScaleHeight);
	}
	@SuppressLint("NewApi")
	@Override 
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);   
		VideoBit.copyPixelsFromBuffer(buffer);
		canvas.drawBitmap(VideoBit, m_Matrix, null);
		buffer.position(0);

	}
	public void SetBitmap()
	{
		postInvalidate();  //ʹ��postInvalidate����ֱ�����߳��и��½���
	}
	public void run()   
	{   

	}

}  
